package priv.ln.project.service;

import priv.ln.project.domain.*;

/**
 * ClassName: NameListService
 * Package: priv.ln.project.service
 * Description:
 *
 * @Author 辣辣
 * @Create 2024/7/17 19:16
 * @Version 1.0
 */
public class NameListService {
//    public static void main(String[] args) {
//
//        NameListService listService = new NameListService();
//        Employee[] employees = listService.getAllEmployees();
//        for (int i = 0; i < employees.length; i++) {
//            System.out.println(employees[i]);
//        }
//
//    }

    private Employee[] employees;

    public NameListService() {
        employees = new Employee[Data.EMPLOYEES.length];

        for (int i = 0; i < employees.length; i++) {

            int employeeNumber = Integer.parseInt(Data.EMPLOYEES[i][0]);
            Equipment equipment = null;
            //创建设备对象
            if (Data.EQUIPMENTS[i].length > 0){
                int equipmentNumber = Integer.parseInt(Data.EQUIPMENTS[i][0]);
                if(equipmentNumber == Data.PC){
                    equipment = new PC(Data.EQUIPMENTS[i][1],Data.EQUIPMENTS[i][2]);
                }else if (equipmentNumber == Data.NOTEBOOK){
                    equipment = new NoteBook(Data.EQUIPMENTS[i][1],Double.parseDouble(Data.EQUIPMENTS[i][2]));
                }else if (equipmentNumber == Data.PRINTER){
                    equipment = new Printer(Data.EQUIPMENTS[i][1],Data.EQUIPMENTS[i][2]);
                }
            }
            //创建员工对象
            if (employeeNumber == Data.EMPLOYEE) {
                employees[i] = new Employee(Integer.parseInt(Data.EMPLOYEES[i][1]),
                        Data.EMPLOYEES[i][2],Integer.parseInt(Data.EMPLOYEES[i][3]),
                        Double.parseDouble(Data.EMPLOYEES[i][4]));
            } else if (employeeNumber == Data.PROGRAMMER) {
                employees[i] = new Programmer(Integer.parseInt(Data.EMPLOYEES[i][1]),
                        Data.EMPLOYEES[i][2],Integer.parseInt(Data.EMPLOYEES[i][3]),
                        Double.parseDouble(Data.EMPLOYEES[i][4]),equipment);
            } else if (employeeNumber == Data.DESIGNER) {
                employees[i] = new Designer(Integer.parseInt(Data.EMPLOYEES[i][1]),
                        Data.EMPLOYEES[i][2],Integer.parseInt(Data.EMPLOYEES[i][3]),
                        Double.parseDouble(Data.EMPLOYEES[i][4]),equipment,
                        Double.parseDouble(Data.EMPLOYEES[i][5]));
            } else if (employeeNumber == Data.ARCHITECT) {
                employees[i] = new Architect(Integer.parseInt(Data.EMPLOYEES[i][1]),
                        Data.EMPLOYEES[i][2],Integer.parseInt(Data.EMPLOYEES[i][3]),
                        Double.parseDouble(Data.EMPLOYEES[i][4]),equipment,
                        Double.parseDouble(Data.EMPLOYEES[i][5]),Integer.parseInt(Data.EMPLOYEES[i][6]));
            }
        }
    }


    /**
     * 获取当前所有员工
     * @return 包含所有员工对象的数组
     */
    public Employee[] getAllEmployees(){
        return employees;
    }

    /**
     * 获取指定ID的员工对象
     * @param id 指定员工的ID
     * @return 指定员工对象
     * @throws TeamException 找不到指定的员工
     */
    public Employee getEmployee(int id) throws TeamException{
        for (int i = 0; i < employees.length; i++) {
            if (employees[i].getId() == id){
                return employees[i];
            }
        }
        throw new TeamException("找不到指定的员工");
    }
}
